package com.notesyncdemo;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;

import com.google.web.bindery.requestfactory.shared.Receiver;
import com.notesyncdemo.shared.NoteSyncDemoRequest;
import com.notesyncdemo.shared.NoteSyncDemoRequestFactory;
import com.notesyncdemo.shared.TaskProxy;



public class AsyncTaskList extends AsyncTask<String[][], Void, String[][] > {
	private static final String TAG = "NoteSyncDemoActivity";
	NoteSyncDemoActivity activity;
	static String tag = "vincent";
	
	public AsyncTaskList(NoteSyncDemoActivity activity) {
		this.activity = activity;
	}

	@Override
	protected String[][] doInBackground(String[][]... params) {
		
		Log.i("vincent", "inside the async");
		String[][] clientArray = params[0];
		
		final List<TaskProxy> list = new ArrayList<TaskProxy>();
		NoteSyncDemoRequestFactory factory = Util.getRequestFactory(activity, NoteSyncDemoRequestFactory.class);
		factory.taskRequest().queryTasks().fire(new Receiver<List<TaskProxy>>() {

			@Override
			public void onSuccess(List<TaskProxy> arg0) {
				Log.i("vincent", "inside succes of Async");
				
				for(TaskProxy taskpro : arg0) {
					Log.d(tag,"name:"+taskpro.getName());
					Log.d(tag, "note:"+taskpro.getNote());
					Log.d(tag, "user:"+taskpro.getEmailAddress());
					Log.d(tag, "created"+taskpro.getCreatedDate());
					Log.d(tag, "modified:"+taskpro.getModifiedDate());
					Log.d(tag,"tag"+taskpro.getTags());
				}
				Log.d(tag, "Size of list"+list.size());
				list.addAll(arg0);
				Log.d(tag, "Size of list"+list.size());
				
			} 
			
			
		});
		
		Log.i("vincent", "async completed taking the list");
		int il = list.size();
		Log.d(TAG, "inside Asynctasklist: size of list returned from server"+list.size());
		if(il==0) { return null;} //if the list size is zero then return the stuff
		
		//String[] notelist = new String[il];// from previous version
		//notelist[0]= "vincent 0";// from previous
		int i=0;
		String[] [] serverArr= new String[il][11];
		
		
		
		for(TaskProxy task: list) {
			//notelist[i]=task.getName(); // from previous
			
			serverArr[i][0]= i+"";
			serverArr[i][1]= task.getTitle();
			serverArr[i][2]= task.getNote();
		    serverArr[i][3]= task.getCreatedDate();
		    serverArr[i][4]= task.getModifiedDate();
		    serverArr[i][5]= task.getTags();
		    serverArr[i][6]= task.getEncrypted();
		    serverArr[i][7]= task.getTheme();
		    serverArr[i][8]= task.getSelectionStart();
			serverArr[i][9]= task.getSelectionEnd();
			serverArr[i][10]= task.getScroll_position();					
			
			
			Log.d(tag,i+" list title:"+task.getTitle());
			Log.d(tag, i+" list note:"+task.getNote());
			Log.d(tag, i+" list user:"+task.getEmailAddress());
			Log.d(tag, i+" list created:"+task.getCreatedDate());
			//Log.i("vincent", "inside the string"+notelist[i]);
			
			i++;
			
		}
		
		
		String[][] addArrayClient = getDifference(clientArray, serverArr);
    	String[][] addArrayServer = getDifference(serverArr,clientArray);
    	
    	//NoteSyncDemoRequestFactory factory = Util.getRequestFactory(activity, NoteSyncDemoRequestFactory.class);
        NoteSyncDemoRequest request = factory.taskRequest();
        
        TaskProxy task = request.create(TaskProxy.class);
        for(int ik=0;ik<addArrayServer.length;ik++) {
        	task = (TaskProxy) request.createTask();
        	
        	
        	//task.set_id(params[i][0]);
        	Log.d(TAG,"inside uploading task:"+ik+":"+addArrayServer[ik][1]);
        	task.setTitle(addArrayServer[ik][1]);
        	task.setNote(addArrayServer[ik][2]);
        	task.setCreatedDate(addArrayServer[ik][3]);
        	task.setModifiedDate(addArrayServer[ik][4]);
        	task.setTags(addArrayServer[ik][5]);
        	task.setEncrypted(addArrayServer[ik][6]);
        	task.setTheme(addArrayServer[ik][7]);
        	task.setSelectionStart(addArrayServer[ik][8]);
        	task.setSelectionEnd(addArrayServer[ik][9]);
        	task.setScroll_position(addArrayServer[ik][10]);
        	
        	// made one task
        	
        	request.updateTask(task).fire();
        }
    	
		Log.d(tag,"going to return the srverARR");
		//activity.setServerArr(serverArr);
		return serverArr;
	}

	@Override
	protected void onPostExecute(String[][] result) {
		Log.d(tag, "inside the post execute");
		activity.updateText("Syncing Finished!");
		activity.makeClickable(true);
		super.onPostExecute(result);
	}
	
	/**
	@Override
	protected void onPostExecute(String[][] serverArr) {
		Log.d(TAG, "onPostExecute() of asynctasklist" );
		activity.setServerArr(serverArr);
	}
	*/
private String[][] getDifference(String[][] smallTuple, String[][] bigTuple) {
		
		// Here we have to find tuple by bigTuple - smallTuple
		// Elements in bigTuple that not in smallTuple has to be returned
		
		//if bigtuple does not have anything then nothing is to be returned
		if(bigTuple==null) {
			Log.d(TAG, "inside the getdifference and returning the null");
			return null;
		}
		
		// if smalltuple is null then return everything from bigtuple
		if(smallTuple==null) {
			Log.d(TAG, "inside the getdifference and returning the bigtple");
			return bigTuple;
		}
		
		ArrayList<String[]> list = new ArrayList<String[]>();
		//3 is for created
		for(int i=0;i<bigTuple.length;i++) {
			boolean flag = false;
			for(int j=0;j<smallTuple.length;j++) {
				String bigDate = bigTuple[i][3];
				String smallDate = smallTuple[j][3];
				
				if(bigDate.equals(smallDate)) {
					flag = true;
				}
				
			}
			
			if(flag) {
				// element is present in both no change
			}
			else {
				list.add(bigTuple[i]);
			}
		}
		
		String[][] retarr = new String[list.size()][];
		for(int k=0;k<list.size();k++) {
			retarr[k]= list.get(k);
			
		}
		Log.d(TAG, "inside the getdifference and returning the retarr");
		return retarr;
	}
	
}
